package com.xuebang.admin.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xuebang.admin.dao.Finder;
import com.xuebang.admin.entity.OperGroupColumn;
import com.xuebang.admin.entity.OperGroupRule;
import com.xuebang.admin.entity.Operator;
import com.xuebang.admin.entity.OperatorGroups;
import com.xuebang.admin.entity.OperatorRules;
import com.xuebang.admin.mapper.OperGroupColumnMapper;
import com.xuebang.admin.mapper.OperGroupRuleMapper;
import com.xuebang.admin.mapper.OperatorGroupsMapper;
import com.xuebang.admin.mapper.OperatorRulesMapper;

@Service
@Transactional
public class OperatorGroupsService {
	private Logger logger = LoggerFactory
			.getLogger(OperatorGroupsService.class);

	@Inject
	private OperatorGroupsMapper operatorGroupsMapper;

	@Inject
	private OperGroupRuleMapper operGroupRuleMapper;

	@Inject
	private OperatorRulesMapper operatorRulesMapper;

	/** 20140211 add by xy start.. */
	@Inject
	private OperGroupColumnMapper operGroupColumnMapper;
	/** 20140211 add by xy end.. */

	@Inject
	Finder finder;

	public List<OperatorGroups> all(boolean fetchOperatorRules) {
		List<OperatorGroups> operatorGroupsList = finder
				.from("operator_groups").all(OperatorGroups.class);
		if (fetchOperatorRules) {
			if (operatorGroupsList != null && operatorGroupsList.size() > 0) {
				for (OperatorGroups operatorGroups : operatorGroupsList) {
					operatorGroups = fetchOperatorRules(operatorGroups);
				}
			}
		}
		return operatorGroupsList;
	}

	private OperatorGroups fetchOperatorRules(OperatorGroups operatorGroups) {
		if (operatorGroups == null) {
			return null;
		}
		List<Map<String, Object>> ruleIdMapList = finder
				.from("oper_group_rule").select("RULE_ID")
				.where("GROUP_ID", operatorGroups.getGroupId()).all();
		if (ruleIdMapList != null && ruleIdMapList.size() > 0) {
			List<Integer> ruleIdList = new ArrayList<Integer>();
			for (Map<String, Object> ruleIdMap : ruleIdMapList) {
				ruleIdList.add((Integer) ruleIdMap.get("RULE_ID"));
			}
			List<OperatorRules> operatorRulesList = finder
					.from("operator_rules").in("RULE_ID", ruleIdList.toArray())
					.all(OperatorRules.class);
			operatorGroups.setOperatorRulesList(operatorRulesList);
		}
		
		//20140211 add by xy start...
		//获得其关联栏目
		String strRelatedColumns = operGroupColumnMapper.searchRelatedColumnsByGID(operatorGroups.getGroupId());
		if(strRelatedColumns!=null && !"".equals(strRelatedColumns.trim())) {
			operatorGroups.setRelatedColumns(strRelatedColumns);
		} else {
			operatorGroups.setRelatedColumns(",");
		}
//		System.out.println("================================");
//		System.out.println("columns:" + strRelatedColumns);
//		System.out.println("================================");
		//20140211 add by xy end...
		return operatorGroups;
	}

	public void create(OperatorGroups operatorGroups) {
		operatorGroupsMapper.insertOperatorGroups(operatorGroups);
		List<OperatorRules> operatorRulesList = operatorGroups
				.getOperatorRulesList();
		if (operatorRulesList != null && operatorRulesList.size() > 0) {
			for (OperatorRules operatorRules : operatorRulesList) {
				OperGroupRule operGroupRule = new OperGroupRule();
				operGroupRule.setGroupId(operatorGroups.getGroupId());
				operGroupRule.setRuleId(operatorRules.getRuleId());
				operGroupRuleMapper.insertOperGroupRule(operGroupRule);
			}
		}

		// 20130211 add by xy start...
		// 增加关联组
		if (operatorGroups != null) {
			OperGroupColumn tmpOperGroupColumn = new OperGroupColumn();
			if (operatorGroups.getGroupId() != null) {
				tmpOperGroupColumn.setGroupId(operatorGroups.getGroupId());
			}
			tmpOperGroupColumn.setColumnIds(operatorGroups.getRelatedColumns());
			this.operGroupColumnMapper.insertGroupColumn(tmpOperGroupColumn);
		}
		// 20130211 add by xy end...
	}

	public void update(OperatorGroups operatorGroups) {
		operatorGroupsMapper.updateOperatorGroups(operatorGroups);
		operGroupRuleMapper.deleteOperGroupRuleByGroupId(operatorGroups
				.getGroupId());
		List<OperatorRules> operatorRulesList = operatorGroups
				.getOperatorRulesList();
		if (operatorRulesList != null && operatorRulesList.size() > 0) {
			for (OperatorRules operatorRules : operatorRulesList) {
				OperGroupRule operGroupRule = new OperGroupRule();
				operGroupRule.setGroupId(operatorGroups.getGroupId());
				operGroupRule.setRuleId(operatorRules.getRuleId());
				operGroupRuleMapper.insertOperGroupRule(operGroupRule);
			}
		}

		// 20140211 add by xy start...
		// 修改关联组
		if (operatorGroups != null) {
			this.operGroupColumnMapper.updateGroupColumn(
					operatorGroups.getRelatedColumns(),
					operatorGroups.getGroupId());
		}
		// 20140211 add by xy end...
	}

	public void delete(Long groupId) {
		List<Operator> operatorList = finder.from("OPERATORS")
				.where("GROUP_ID", groupId).all(Operator.class);
		if (operatorList != null && operatorList.size() > 0) {
			throw new RuntimeException("该角色下已存在账户，不能删除！");
		} else {
			operGroupRuleMapper.deleteOperGroupRuleByGroupId(groupId);
			operatorGroupsMapper.deleteOperatorGroups(groupId);
			// 20140211 add by xy start..
			operGroupColumnMapper.deleteGroupColumn(groupId);
			// 20140211 add by xy end..
		}
	}

	public OperatorGroups findById(Long groupId) {
		OperatorGroups operatorGroup = finder.find(OperatorGroups.class,
				"group_Id", groupId);
		operatorGroup = fetchOperatorRules(operatorGroup);
		return operatorGroup;
	}
	
	public List<OperatorGroups> findByName(String groupName){

		 if(groupName!=null){
			return operatorGroupsMapper.findByName(groupName);
		 } else {
			 return null;
		 }
	}

}
